from django.db import models

# Create your models here.
class Server(models.Model):
    name = models.CharField(max_length=20,verbose_name='服务器名称')
    path = models.CharField(max_length=32,verbose_name='服务器根路径')

    class Meta:
        db_table = 'api_server'
        verbose_name = '服务器'
        verbose_name_plural = verbose_name
        ordering = ['-id']

    def __str__(self):
        return self.name

class Api_Route(models.Model):
    name = models.CharField(max_length=50,verbose_name='请求接口名称')
    path = models.CharField(max_length=100,verbose_name='接口路由')
    ''' 
    on_delete  models.CASCADE      删除关联数据，与之关联的也删除
                models.DO_NOTHING   删除关联数据什么都不做
                models.PROTECT      删除关联数据，引发错误 protectedError
                models.SET_NULL     删除关联数据，与之关联的数据设置为NULL
    '''
    server = models.ForeignKey(Server,verbose_name='服务器',on_delete=models.CASCADE)

    class Meta:
        db_table = 'api_route'
        verbose_name = '接口路由'
        verbose_name_plural = verbose_name
        ordering = ['-id']

    def __str__(self):
        return self.name

class Response(models.Model):
    name = models.CharField(max_length=20,verbose_name='响应名称')
    json_result = models.TextField(verbose_name='返回结果')
    delay = models.IntegerField(default=1,verbose_name='响应延迟')
    router = models.ForeignKey(Api_Route,verbose_name='响应接口',on_delete=models.CASCADE)

    class Meta:
        db_table = 'api_response'
        verbose_name = '接口响应'
        verbose_name_plural = verbose_name
        ordering = ['-id']

    def __str__(self):
        r = self.router
        a = r.server
        return a.name + '->' + r.name + '->' + self.name